{extend name="lib/main" /}
{block name="title"}{/block}
{block name="css"}
{load href="/static/sys/css/common.css" /}
{load href="/static/plugins/select2/css/select2.css" /}
{load href="/static/plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.css" /}

{load href="/static/sys/css/skin_0.css" /}
<style>
    .upfile i{padding-right:3px !important;}
    .file{ position:absolute;right:0;top:0;width:100px;height:100px;opacity:0; ilter:alpha(opacity=0);cursor:pointer;margin-bottom: 0px;margin-right: 0px; }
</style>
{/block}
{block name="javascript"}
{load  href="/static/plugins/select2/js/select2.js" /}
{load  href="/static/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js" /}
{load  href="/static/plugins/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js" /}

<script>
    $(document).ready(function () {
        $("#ok").bind("click", Save);
        $("#cancel").bind("click", function () {
            parent.layer.close(parent.layer_add_index);
        });

        /**
         * 保存内容
         */
        function Save() {
            var load_index = layer.load(0, { shade: false });

            $.ajax({
                type: 'POST',
                url: "{:url('permission/add')}",
                data: $("#fmNew").serialize(),
                dataType: 'json',
                success: function (data) {
                    if (data.code == 1) {
                        layer.close(load_index);
                        layer.alert(data.msg, function () {
                            parent.refreshtb();
                            //window.close();
                            parent.layer.close(parent.layer_add_index);
                        });
                    } else {
                        layer.alert(data.msg);
                    }
                }
            })
        }

        $('#p_id').select2();
    });

    /**
     * 新增图片
     */
    var layer_image_add;
    function permissionImg() {
        layer_image_add = layer.open({
            type: 2,
            title: "新增图片",
            area: ['70%', '90%'],
            content: "{:url('upload/image')}?tag=permission_img"
        })
    }

    // 上传成功后的回调，该函数名不能写错
    function getImageUrl(res) {
        if (res.tag == 'permission_img') {
            $('#img_url').val(res.url);
            $('#previewImg_permission').attr('src', res.url);
        }
    }

    function pidValue() {
        var pid = $("#p_id").children('option:selected').val();
        if (pid.length === 0) {
            $('#child_permission')[0].removeAttribute('hidden');
            var input = $('#child_permission>div>input');
            input.each(function() {
                $(this).attr('checked', 'checked');
            });

        } else {
            $('#child_permission').attr('hidden', 'hidden');
            var input = $('#child_permission>div>input');
            input.each(function() {
                this.removeAttribute('checked');
            });
        }
    }

</script>
{/block}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form method="post" id="fmNew" class="form-horizontal" enctype="multipart/form-data">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">权限名：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="name" placeholder="请输入名称">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">权限路由：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="route" id="route">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">父级权限：</label>
                            <div class="col-sm-8">
                                <select class="form-control" name="p_id" id="p_id" onchange="pidValue()">
                                    <option value="">请选择父级权限...</option>
                                    {volist name="pid_list" id="v"}
                                    <option value="{$v.id}">{$v.name}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group" id="child_permission">
                            <label class="col-sm-2 control-label">默认子权限：</label>
                            <div class="col-sm-8">
                                {volist name="child_list" id="v"}
                                {$v}&nbsp;<input name="child[]" value="{$key}" type="checkbox" checked="checked"/> &nbsp;
                                {/volist}
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <!--<div class="form-group">
                            <label class="col-sm-2 control-label">引导图(750*1160)：</label>

                            <div class="col-sm-1">
                                <span class="btn btn-primary btn-sm" style="margin-bottom:0;padding:6px 8px;" onclick="permissionImg()">
                                    <i class="glyphicon glyphicon-folder-open" style="padding-right:3px;"></i> 引导图<input type="hidden" name="img_url" id="img_url" />
                                </span>
                            </div>
                            <div class="col-sm-1">
                                <img id="previewImg_permission" style="height:34px;float: right;">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>-->

                        <!--<div class="form-group">
                            <label class="col-sm-2 control-label">权限等级：</label>
                            <div class="col-sm-8">
                                <select class="form-control" name="level" id="level">
                                    {volist name="level_list" id="v"}
                                    <option value="{$key}">{$v}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>-->

                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态：</label>
                            <div class="col-sm-8">
                                <select class="form-control" name="status" id="status">
                                    {volist name="status_list" id="v"}
                                    <option value="{$key}">{$v}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>

                        <div id = 'hidden_input'>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-12 col-sm-offset-5">
                                <button class="btn btn-primary" id="ok" type="button">提交</button>
                                <button class="btn btn-white" id="cancel" type="button">取消</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}